package com.example.siyun.controller.usercontroller;

import com.example.siyun.pojo.UserList;
import com.example.siyun.service.userserver.UserDeveService;
import com.example.siyun.util.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * @Author 赵薇
 * @Date 2022/10/12 15:02
 * @PackageName:com.example.siyun.controller
 * @ClassName: UserDeveController  用户列表 控制层
 * @Description: TODO
 * @Version 1.0
 */
@RestController
@Slf4j
public class UserDeveController {

    @Autowired
    private UserDeveService userDeveServer;

    /**
     * 全查用户
     * @param page
     * @param limit
     * @param userAccount
     * @param userNickname
     * @param createDate
     * @param idCard
     * @param phone
     * @param userEmail
     * @param profession
     * @return
     */
    @RequestMapping("/Primary/getAllz")
    private PageResult getAll(@RequestParam(defaultValue = "5") Integer page, @RequestParam(defaultValue = "1") Integer limit,
                              String userAccount, String userNickname, String createDate, String idCard, String phone, String userEmail, String profession){

        PageHelper.startPage(page,limit);

        List<UserList> all = userDeveServer.getAll(userAccount,userNickname,createDate,idCard,phone,userEmail,profession);
        PageInfo<UserList> pageInfo = new PageInfo<>(all);

        PageResult pageResult = new PageResult();
        pageResult.setData(pageInfo.getList());
        pageResult.setMessage("成功");
        pageResult.setState(0);
        pageResult.setTotal(pageInfo.getTotal());
        return pageResult;
    }

    /**
     * 是否为进思用户查询
     * @param page
     * @param limit
     * @param isd
     * @param userAccount
     * @param userNickname
     * @param createDate
     * @param idCard
     * @param phone
     * @param userEmail
     * @param profession
     * @return
     */
    @RequestMapping("/Primary/getUser")
    private PageResult getUser(@RequestParam(defaultValue = "5") Integer page,@RequestParam(defaultValue = "1") Integer limit,
                               int isd,String userAccount,String userNickname,String createDate,String idCard,String phone,String userEmail,String profession){

        PageHelper.startPage(page,limit);

        List<UserList> all = userDeveServer.getUser(isd,userAccount,userNickname,createDate,idCard,phone,userEmail,profession);
//        System.out.println(userAccount);
        PageInfo<UserList> pageInfo = new PageInfo<>(all);

        PageResult pageResult = new PageResult();
        pageResult.setData(pageInfo.getList());
        pageResult.setMessage("成功");
        pageResult.setState(0);
        pageResult.setTotal(pageInfo.getTotal());
        return pageResult;
    }

    /**
     * 封号日志查询
     * @param page
     * @param limit
     * @param cdate
     * @return
     */
    @RequestMapping("/Primary/getClo")
    private PageResult getClo(@RequestParam(defaultValue = "5") Integer page,@RequestParam(defaultValue = "1") Integer limit,
                              Integer cdate){
        PageHelper.startPage(page,limit);

        List<UserList> all = userDeveServer.getClo(cdate);
        PageInfo<UserList> pageInfo = new PageInfo<>(all);

        PageResult pageResult = new PageResult();
        pageResult.setData(pageInfo.getList());
        pageResult.setMessage("成功");
        pageResult.setState(0);
        pageResult.setTotal(pageInfo.getTotal());
        return pageResult;
    }

    /**
     * 高级检索查询
     * @param user
     * @return
     */
    @RequestMapping("/Primary/seniorCat")
    public PageResult seniorCat(UserList user){
        log.info("高级检索：user:{}",user);
        userDeveServer.seniorCat(user);

        PageResult result = new PageResult();
        result.setMessage("成功");
        log.info("高级检索：user:{},msg:{}",user,result.getMessage());
        return result;
    }

    /**
     * 编辑用户
     * @param userList
     * @return
     */
    @RequestMapping("/Primary/editz")
    public PageResult edit(UserList userList){
        log.info("编辑用户：userList:{}",userList);
        userDeveServer.edit(userList);
        PageResult result = new PageResult();
        result.setMessage("成功");
        log.info("高级检索：userList:{},msg:{}",userList,result.getMessage());
        return result;
    }

    /**
     * 封号结束时间
     * @param userList
     * @param closureDays
     * @return
     * @throws ParseException
     */
    @RequestMapping("/advanced/indate")
    public PageResult indate(UserList userList,int closureDays) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:MM:SS");
        // 获取当前时间
        Date date=new Date();
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        String t = simpleDateFormat.format(calendar.getTime());
        // 把日期往后增加一天,整数  往后推,负数往前移动
        calendar.add(Calendar.DATE, closureDays);
        // 这个时间就是日期往后推一天的结果
        String  t_2 = simpleDateFormat.format(calendar.getTime());
        //parse ：转换类型  String --> Date
        Date tt = simpleDateFormat.parse(t);
        Date tt_2 = simpleDateFormat.parse(t_2);

        userList.setClonowDate(tt);
        userList.setCloendDate(tt_2);

        System.out.println("userList : "+userList);

        userDeveServer.indate(userList);

        PageResult result = new PageResult();
        result.setMessage("成功");
        return result;
    }

    /**
     * 添加封禁原因
     * @param userList
     * @return
     */
    @RequestMapping("/advanced/banned")
    public PageResult banned(UserList userList){
        userDeveServer.banned(userList);

        PageResult<Object> result = new PageResult<>();
        result.setMessage("成功");
        return result;
    }

    /**
     * 封禁Switch
     * @param userId    封禁的id
     * @param statuss  封禁的状态
     * @return
     */
    @RequestMapping("/advanced/upStatuss")
    public PageResult upStatuss(int userId,String statuss){

        log.info("封禁用户：userId:{},statuss:{}",userId,statuss);
        userDeveServer.upStatuss(userId,statuss);

        PageResult result = new PageResult();
        result.setMessage("成功");
        log.info("封禁成功");
        return result;
    }

    /**
     * 封号switch
     * @param userId
     * @param cdate
     * @return
     */
    @RequestMapping("/advanced/cdate")
    public PageResult cdate(int userId,Integer cdate){
        log.info("封号：userId:{},cdate:{}",userId,cdate);

        userDeveServer.cdate(userId,cdate);

        PageResult result = new PageResult();
        result.setMessage("成功");
        log.info("封号成功");
        return result;
    }


    /**
     * 修改用户（进思/非进思）
     * @param userId
     * @return
     */
    @RequestMapping("/advanced/updateIsd")
    public PageResult updateIsd(@RequestBody String[] userId){
        log.info("编辑用户：userId:{}",userId);
        for (String string : userId) {
            int js = 0 ;
            userDeveServer.updateIsd(string,js);
        }

        PageResult result = new PageResult();
        result.setMessage("成功");
        log.info("修改成功！！");
        return result;
    }

    /**
     * 查询零消费用户
     * @param page
     * @param limit
     * @param userAmountu
     * @param userAccount
     * @param userNickname
     * @param createDate
     * @return
     */
    @RequestMapping("/advanced/getZero")
    private PageResult getZero(@RequestParam(defaultValue = "10") Integer page,@RequestParam(defaultValue = "1") Integer limit,
                               String userAmountu, String userAccount,String userNickname,String createDate){

        HashMap<String, Object> map = new HashMap<>();
        PageHelper.startPage(page,limit);

        List<UserList> all = userDeveServer.getZero(userAmountu,userAccount,userNickname,createDate);
        PageInfo<UserList> pageInfo = new PageInfo<>(all);

        PageResult pageResult = new PageResult();
        pageResult.setData(pageInfo.getList());
        pageResult.setMessage("成功");
        pageResult.setState(0);
        pageResult.setTotal(pageInfo.getTotal());
        return pageResult;
    }

    /**
     * 查询封禁用户
     * @param page
     * @param limit
     * @param statuss
     * @param userAccount
     * @param userNickname
     * @param createDate
     * @return
     */
    @RequestMapping("/advanced/getAtu")
    private PageResult getAtu(@RequestParam(defaultValue = "10") Integer page,@RequestParam(defaultValue = "1") Integer limit,
                              Integer statuss,String userAccount,String userNickname,String createDate){

        HashMap<String, Object> map = new HashMap<>();
        PageHelper.startPage(page,limit);

        List<UserList> all = userDeveServer.getAtu(statuss,userAccount,userNickname,createDate);
        PageInfo<UserList> pageInfo = new PageInfo<>(all);

        PageResult pageResult = new PageResult();
        pageResult.setData(pageInfo.getList());
        pageResult.setMessage("成功");
        pageResult.setState(0);
        pageResult.setTotal(pageInfo.getTotal());
        return pageResult;
    }

    /**
     * 优惠券和用户两表联查
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/Primary/getCoupons")
    public PageResult getApp(@RequestParam(defaultValue = "10") Integer page,@RequestParam(defaultValue = "1") Integer limit){

        HashMap<String, Object> map = new HashMap<>();
        PageHelper.startPage(page,limit);

        List<UserList> coupons = userDeveServer.getCoupons();
        PageInfo<UserList> pageInfo = new PageInfo<>(coupons);

        PageResult pageResult = new PageResult();
        pageResult.setData(pageInfo.getList());
        pageResult.setMessage("成功");
        pageResult.setState(0);
        pageResult.setTotal(pageInfo.getTotal());
        return pageResult;
    }

}
